package com.chenglu.android.mine.member.mvp.presenter;





import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.AmountTypeBean;
import com.chenglu.android.mine.bean.FansListBean;
import com.chenglu.android.mine.bean.MemberCenterBean;
import com.chenglu.android.mine.bean.MoneyBean;
import com.chenglu.android.mine.bean.PosterBean;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.member.mvp.contract.MemberContract;
import com.chenglu.android.mine.member.mvp.model.MemberModel;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import retrofit2.http.POST;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class MemberPresenter extends BasePresenter<MemberContract.View> implements MemberContract.MemberPresenter {

     private  MemberContract.MemberModel model;

    public MemberPresenter(){
        model = new MemberModel();
    }



    @Override
    public void getLevelDescriptions(String token) {
        if(!isViewAttached()){
            return;
        }

        model.getLevelDescriptions(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ReturnStringBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull ReturnStringBean bean) {
                        //每次进来都会刷新token
                        if(bean.getCode().equals(Constant.HTTPCODE200)){
                            mView.getLevelDescriptions(bean);
                        }else {
                            mView.getLevelDescriptions(bean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
//                        apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }





    @Override
    public void getMemberCenterInfo(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getMemberCenterInfo(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<MemberCenterBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull MemberCenterBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getMemberCenterInfo(bean);
                        }else {
                          mView.getMemberCenterInfo(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getPromotionPoster(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getPromotionPoster(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PosterBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull PosterBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getPromotionPoster(bean);
                        }else {
                            mView.getPromotionPoster(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getFansPage(String token, int current, int size) {
        if(!isViewAttached()){
            return;
        }
        model.getFansPage(token,current,size)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<FansListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull FansListBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getFansPage(bean);
                        }else {
                            mView.getFansPage(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getConsumerPage(String token, int current, int size) {
        if(!isViewAttached()){
            return;
        }
        model.getConsumerPage(token,current,size)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<FansListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull FansListBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getConsumerPage(bean);
                        }else {
                            mView.getConsumerPage(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


}
